﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NLBS.Entity;
using System.Xml;
using NLBS.Data.Business;

namespace NLBS.Blog.Plugins.chili
{
    public partial class chiliCodeHeighter : System.Web.UI.UserControl, IPlugin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Page.ClientScript.RegisterClientScriptInclude("chili", "/plugins/jquery.chili.pack.js");
        }

        #region IPlugin Members

        public void Install()
        {
            Widget widget = new Widget();
            widget.Hidden = false;
            widget.Type = 1;
            widget.Title = "chiliCodeHeighter";
            widget.Name = "chiliCodeHeighter";
            widget.Order = 1;
            widget.InstallTime = DateTime.Now;
            widget.Content = "$chili$";
            WidgetData.InsertWidget(widget);
            XmlDocument doc = SiteConfig.GetExtensionsXml();
            XmlNode root = doc.DocumentElement;

            XmlElement elem = doc.CreateElement("PluginID");
            elem.InnerText = widget.WidgetID.ToString();

            XmlNode plugin = root.SelectSingleNode(string.Format("descendant::Plugin[Name='{0}']", widget.Title));
            plugin.AppendChild(elem);
            XmlNode name = plugin.SelectSingleNode("descendant::Enabled");
            name.InnerText = "False";

            SiteConfig.SaveExtensionsXml(doc);
        }

        public void UnInstall()
        {
            XmlDocument doc = SiteConfig.GetExtensionsXml();
            XmlNode root = doc.DocumentElement;
            XmlNode plugin = root.SelectSingleNode("descendant::Plugin[Name='chiliCodeHeighter']");
            XmlNode PluginIDNode = plugin.SelectSingleNode("descendant::PluginID");
            int widgetID = Convert.ToInt32(PluginIDNode.InnerText);
            WidgetData.DeleteWidget(widgetID);
            plugin.SelectSingleNode("descendant::Enabled").InnerText = "True";
            plugin.RemoveChild(PluginIDNode);
            SiteConfig.SaveExtensionsXml(doc);
        }

        public void Update()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}